package han.cirno.corrupt.components.calendar

import han.cirno.corrupt.data.instances.UserClassesData
import java.util.Calendar

class ClassesTimeLine {
    companion object{
        fun fromTable():ClassesTimeLine{
            val classTable = UserClassesData.tryFetchClassTable()
            val firstDay = UserClassesData.tryFetchFirstDay()
            val classesTimeLine = ClassesTimeLine()
            if(classTable==null||firstDay==0L)return classesTimeLine
            val calendar = Calendar.getInstance()
            val currentWeeks = CalendarUtil.calcCurrentWeeks(firstDay, calendar)+1
            classTable.classEntries.forEach {
                if(!it.weeks.contains(currentWeeks)||
                    it.dow!= UserClassesData.DayOfWeek.valueOfJavaCalendarConst(
                        calendar.get(Calendar.DAY_OF_WEEK)))return@forEach
                var j=0
                for (i in it.time.from..it.time.to){
                    val (from, to) = CalendarUtil.fromClassTimesToSeconds(i)
                    classesTimeLine.entries.add(Entry(from,to,j++,it.time.to-it.time.from+1,it))
                }
            }
            classesTimeLine.entries.sortWith{ a, b->a.fromSecond.toInt()-b.fromSecond.toInt()}
            return classesTimeLine
        }
    }

    val entries=ArrayList<Entry>()

    fun getCurrentState(nowSecond:Int):ClassState?{
        /*val nowSecond =
            nowCalendar[Calendar.HOUR_OF_DAY] * 60 * 60 + nowCalendar[Calendar.MINUTE] * 60 + nowCalendar[Calendar.SECOND]*/
        if(entries.size==0)
            return ClassState(null,-2,-2,false,0,0)
        val firstEntry=entries[0]
        val lastEntry=entries[entries.size-1]
        if (nowSecond<firstEntry.fromSecond)
            return ClassState(firstEntry.classEntry,firstEntry.fromSecond-nowSecond,firstEntry.fromSecond,false,0,0)
        if (nowSecond>lastEntry.toSecond)
            return ClassState(null,-1,-1,false,0,0)
        entries.forEachIndexed{i,v->
            if (nowSecond in v.fromSecond..v.toSecond)
                return ClassState(
                    v.classEntry,
                    v.toSecond - nowSecond,
                    v.toSecond - v.fromSecond,
                    true,
                    v.phase,
                    v.totalPhase
                )
            if(i!=entries.size-1) {
                val nextEntry = entries[i + 1]
                if(nowSecond in v.toSecond..nextEntry.fromSecond)
                    return ClassState(
                        nextEntry.classEntry,
                        nextEntry.fromSecond - nowSecond,
                        nextEntry.fromSecond - v.toSecond,
                        false,
                        v.phase,
                        v.totalPhase
                    )
            }
        }
        return null
    }

    data class Entry(
        val fromSecond:Long,
        val toSecond:Long,
        val phase:Int,
        val totalPhase: Int,
        val classEntry: UserClassesData.ClassEntry
    )

    data class ClassState(
        val classEntry: UserClassesData.ClassEntry?,
        val timeLeft: Long,
        val timeTotal: Long,
        val isInClass: Boolean,
        val phase: Int,
        val totalPhase: Int
    )
}